home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-06-19 | 7.1 KB | 311 lines |
- package symantec.itools.util;
-
-
- import java.awt.Component;
- import java.awt.Event;
-
-
- /**
- *
- * Sets a timer to wait before an action event is posted to a component.
- * The caller can specify the target component, the event to send to the
- * component, and the time delay.
- *
- * The timer is implemented as a thread. The one of the start(...) methods should
- * be called to start the thread.
- *
- *
- * @version 1.0, Nov 26, 1996
- *
- * @author Symantec
- *
- */
-
-
- public class Timer
- implements Runnable
- {
- Component target;
- int eventType;
- boolean repeat;
- boolean repeating;
- boolean execute;
- Thread thread;
- int delay;
-
- /**
- * Create default timer. This timer will have a 1000 millisecond delay
- * and an ACTION_EVENT, and will not repeat.
- *
- * @param t component which is the target of the timer. This component
- * will receive the specified event when time elapses.
- *
- */
-
- public Timer(Component t)
- {
- this(t, 1000);
- }
-
- /**
- * Create timer with specified delay. This timer will use the specified
- * delay and will generate an ACTION_EVENT. It will not repeat.
- *
- * @param t component which is the target of the timer. This component
- * will receive the specified event when time elapses
- *
- * @param d delay in milliseconds
- *
- */
-
- public Timer(Component t, int d)
- {
- this(t, d, false);
- }
-
- /**
- * Create timer with specified delay and repeat setting.
- * After the specified delay this timer will generate
- * an ACTION_EVENT. It will may repeat.
- *
- * @param t component which is the target of the timer. This component
- * will receive the specified event when time elapses
- *
- * @param d delay in milliseconds
- *
- * @param r reset and repeat after generating the event
- *
- */
-
- public Timer(Component t, int d, boolean r)
- {
- this(t, d, r, Event.ACTION_EVENT);
- }
-
- /**
- * Create timer with specified delay, repeat setting, and event.
- * After the specified delay this timer will generate
- * the sepecified event. It will may repeat.
- *
- * @param t component which is the target of the timer. This component
- * will receive the specified event when time elapses
- *
- * @param d delay in milliseconds
- *
- * @param r reset and repeat after generating the event
- *
- * @param e event to send to the target component when time elapses
- *
- * @see java.awt.Event
- *
- */
-
- public Timer(Component t, int d, boolean r, int e)
- {
- target = t;
- delay = d;
- repeat = r;
- execute = false;
- thread = new Thread(this);
- eventType = e;
- thread.start();
- }
-
- /**
- * Set or change the type of event the timer will produce.
- * @param type type of event to be generated by the timer the next
- * time the delay time elapses
- *
- * @see java.awt.Event
- */
-
- public void setEventType(int type)
- {
- eventType = type;
- }
-
-
- /**
- * Get the type of event the timer will produce.
- * @return Type of event to be generated by the timer the next
- * time the delay time elapses.
- *
- * @see java.awt.Event
- */
-
- public int getEventType()
- {
- return eventType;
- }
-
- /**
- * Change the component that is the target of this timer.
- * This is the component which will receive the event when the
- * time elapses
- *
- * @param t component which will be the target of the timer. This component
- * will receive the specified event when time elapses
- *
- */
- public void setTarget(Component t)
- {
- target = t;
- }
-
- /**
- * Get the component that is the target of this timer.
- *
- * @return The component that is currently the target of the timer.
- *
- */
-
- public Component getTarget()
- {
- return target;
- }
-
- /**
- * Set the delay time for this timer.
- * @param d delay in milliseconds. This delay will be used starting
- * after the current delay elapses
- *
- */
- public void setDelay(int d)
- {
- delay = d;
- }
-
- /**
- * Obtain the delay time setting for this timer.
- * @return The current delay setting for this timer.
- * The delay time is in milliseconds
- *
- */
- public int getDelay()
- {
- return delay;
- }
-
- /**
- * Start the timer with existing settings.
- */
-
- public void start()
- {
- execute = true;
- thread.resume();
- }
-
- /**
- * Change the repeat setting of the timer.
- * If the repeat setting is false a single event will be generated. When
- * set to true the timer produces a series of events.
- *
- * @param r reset and repeat after generating the event
- */
-
- public void setRepeat(boolean f)
- {
- repeat = f;
- }
-
- /**
- * Obtain the repeat setting of the timer.
- *
- * @return true if this timer is set to repeat, false if this timer does not repeat
- */
-
- public boolean getRepeat()
- {
- return repeat;
- }
-
- /**
- * Start the timer using the specified delay
- *
-
- *
- */
-
- public void start(int d)
- {
- delay = d;
-
- start();
- }
-
- /**
- * Start the timer using the specified repeat setting.
- *
- * @param r reset and repeat after generating the event
- *
- */
-
- public void start(boolean r)
- {
- repeat = r;
-
- start();
- }
-
- /**
- * Start the timer using the specified delay and repeat settings.
- *
- * @param d delay in milliseconds
- * @param r reset and repeat after generating the event
- *
- */
-
- public void start(int d, boolean r)
- {
- delay = d;
- repeat = r;
-
- start();
- }
-
- /**
- * Stop the timer. After return the timer will generate no more events.
- */
-
- public void stop()
- {
- execute = false;
- repeating = false;
- }
-
- /**
- * Thread body. This method is called by the Java virtual machine in response to a
- * start call by the user.
- * @see #start()
- */
-
- public void run()
- {
- if(!execute) thread.suspend();
- try
- {
- while(true)
- {
- repeating = repeat;
-
- do
- {
- thread.sleep(delay);
-
- if (execute)
- {
- target.handleEvent(new Event(this, eventType, null));
- }
- }
- while (repeating);
-
- thread.suspend();
- }
- }
- catch (InterruptedException e)
- {
- }
- }
- }
-